package com.kty.studyassembly.activity;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kty.studyassembly.R;
import com.kty.studyassembly.activity.base.BaseActivity;
import com.kty.studyassembly.view.fragment.home.HomeFragment;
import com.kty.studyassembly.view.fragment.home.MessageFragment;
import com.kty.studyassembly.view.fragment.home.MineFragment;

/**
 * @function 管理切换fragment
 */
public class HomeActivity extends BaseActivity implements View.OnClickListener {

    private FragmentManager fm;
    private HomeFragment mHomeFragment;
    private MessageFragment mMessageFragment;
    private MineFragment mMineFragment;

    private TextView home_image_view;//主页
    private TextView message_image_view;//消息
    private TextView mine_image_view;//我的

    private RelativeLayout mHomeLayout;
    private RelativeLayout mPondLayout;
    private RelativeLayout mMessageLayout;
    private RelativeLayout mMineLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_home_layout);
        initView();

        mHomeFragment = new HomeFragment();
        fm = getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(R.id.content_layout, mHomeFragment);
        fragmentTransaction.commit();
    }

    //初始化控件
    private void initView() {
        mHomeLayout = (RelativeLayout) findViewById(R.id.home_layout_view);
        mHomeLayout.setOnClickListener(this);
        mPondLayout = (RelativeLayout) findViewById(R.id.pond_layout_view);
        mPondLayout.setOnClickListener(this);
        mMessageLayout = (RelativeLayout) findViewById(R.id.message_layout_view);
        mMessageLayout.setOnClickListener(this);
        mMineLayout = (RelativeLayout) findViewById(R.id.mine_layout_view);
        mMineLayout.setOnClickListener(this);


        home_image_view = (TextView) findViewById(R.id.home_image_view);
        home_image_view.setBackgroundResource(R.mipmap.comui_tab_home_selected);

        message_image_view = (TextView) findViewById(R.id.message_image_view);

        mine_image_view = (TextView) findViewById(R.id.mine_image_view);
    }


    //隐藏
    private void hideFragment(Fragment fragment, FragmentTransaction ft) {
        if (fragment != null) {
            ft.hide(fragment);
        }
    }

    //切换Fragment的逻辑
    @Override
    public void onClick(View view) {

        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        if (view.getId() == R.id.home_layout_view) {
            home_image_view.setBackgroundResource(R.mipmap.comui_tab_home_selected);
            message_image_view.setBackgroundResource(R.mipmap.comui_tab_message);
            mine_image_view.setBackgroundResource(R.mipmap.comui_tab_person);

            home_image_view.setTextColor(Color.parseColor("#ffff00"));
            message_image_view.setTextColor(Color.parseColor("#000000"));
            mine_image_view.setTextColor(Color.parseColor("#000000"));

            hideFragment(mMessageFragment,fragmentTransaction);
            hideFragment(mMineFragment,fragmentTransaction);

            if (mHomeFragment==null){
                mHomeFragment=new HomeFragment();
                fragmentTransaction.add(R.id.content_layout, mHomeFragment);
            }else{
                fragmentTransaction.show(mHomeFragment);
            }

        } else if (view.getId() == R.id.message_layout_view) {
            home_image_view.setBackgroundResource(R.mipmap.comui_tab_home);
            message_image_view.setBackgroundResource(R.mipmap.comui_tab_message_selected);
            mine_image_view.setBackgroundResource(R.mipmap.comui_tab_person);
            home_image_view.setTextColor(Color.parseColor("#000000"));
            message_image_view.setTextColor(Color.parseColor("#ffff00"));
            mine_image_view.setTextColor(Color.parseColor("#000000"));


            hideFragment(mHomeFragment,fragmentTransaction);
            hideFragment(mMineFragment,fragmentTransaction);

            if (mMessageFragment==null){
                mMessageFragment=new MessageFragment();
                fragmentTransaction.add(R.id.content_layout, mMessageFragment);
            }else{
                fragmentTransaction.show(mMessageFragment);
            }


        } else if (view.getId() == R.id.mine_layout_view) {
            home_image_view.setBackgroundResource(R.mipmap.comui_tab_home);
            message_image_view.setBackgroundResource(R.mipmap.comui_tab_message);
            mine_image_view.setBackgroundResource(R.mipmap.comui_tab_person_selected);

            home_image_view.setTextColor(Color.parseColor("#000000"));
            message_image_view.setTextColor(Color.parseColor("#000000"));
            mine_image_view.setTextColor(Color.YELLOW);

            hideFragment(mMessageFragment,fragmentTransaction);
            hideFragment(mHomeFragment,fragmentTransaction);

            if (mMineFragment==null){
                mMineFragment=new MineFragment();
                fragmentTransaction.add(R.id.content_layout, mMineFragment);
            }else{
                fragmentTransaction.show(mMineFragment);
            }


        }

        fragmentTransaction.commit();
    }
}
